package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.entity.Order;
import com.andrew.entity.OrderBeanForAdmin;
import com.andrew.service.OrderService;
import com.andrew.service.impl.OrderServiceImpl;
import com.google.gson.Gson;

public class GetAllOrderServlet extends HttpServlet {
	private OrderService orderService=new OrderServiceImpl();
	private Gson gson=new Gson();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置输出编码
		resp.setContentType("text/html;charset=UTF-8");
		
		try {
			//获取全都订单详情
			List<OrderBeanForAdmin> allOrdersForAdmin=new ArrayList<>();
			List<Order> orders=orderService.getAllOrders();
			for(Order order:orders) {
				OrderBeanForAdmin orderBeanForAdmin=new OrderBeanForAdmin();
				orderBeanForAdmin.setAddress(order.getAddress());
				orderBeanForAdmin.setCreateDate(order.getCreateDate());
				orderBeanForAdmin.setOrderId(order.getOrderId());
				orderBeanForAdmin.setSTATUS(order.getSTATUS());
				orderBeanForAdmin.setTotalPrice(order.getTotalPrice());
				orderBeanForAdmin.setUserName(order.getCustomer().getUserName());
				orderBeanForAdmin.setVC(order.getVC());
				allOrdersForAdmin.add(orderBeanForAdmin);
			}
			
			//封装为json
			String json =gson.toJson(allOrdersForAdmin).toString();
			//输出
			resp.getWriter().write(json);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
